package cn.edu.ctbu.scoremg.api;

import cn.edu.ctbu.scoremg.constant.REnum;
import cn.edu.ctbu.scoremg.entity.Score;
import cn.edu.ctbu.scoremg.exception.RException;
import cn.edu.ctbu.scoremg.service.ScoreService;
import cn.edu.ctbu.scoremg.util.RUtil;
import cn.edu.ctbu.scoremg.vo.QueryObj;
import cn.edu.ctbu.scoremg.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/score")
public class ScoreApiController {
    @Autowired
    private ScoreService scoreService;

    @GetMapping("/list")
    public R<List<Score>> findAll() {
        List<Score> scores = scoreService.getAll();
        return RUtil.success(scores);
    }
    @GetMapping("/list0")
    public R<List<Score>> findAll0() {
        List<Score> scores = scoreService.getAll();
        return RUtil.success(scores);
        //此时学生登录系统
    }

    @GetMapping("/{id}")
    public R<Score> findById(@PathVariable String id) {
        Score score = scoreService.findById(id);
        return RUtil.success(score);
    }

    @GetMapping("/findByStudent")
    public R<List<Score>> findBySno(@RequestParam String sno) {
        return RUtil.success(scoreService.findBySno(sno));
    }

    @GetMapping("/findByCourse")
    public R<List<Score>> findByCname(@RequestParam String cname) {
        return RUtil.success(scoreService.findByCname(cname));
    }

    @GetMapping("/findByStudentAndCourse")
    public R<List<Score>> findBySnoAndCname(@RequestParam String sno, @RequestParam String cname) {
        return RUtil.success(scoreService.findBySnoAndCname(sno, cname));
    }

    @PostMapping("/add")
    public R<Score> add(@RequestBody Score score) {
        return RUtil.success(scoreService.add(score));
    }

    @PutMapping("/update")
    public R<Score> update(@RequestBody Score score) {
        return RUtil.success(scoreService.update(score));
    }

    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable String id) {
        scoreService.delete(id);
        return RUtil.success();
    }

    @PostMapping("/deleteBatch")
    public R deleteBatch(@RequestBody List<String> ids) {
        try {
            scoreService.deleteBatch(ids);
            return RUtil.success();
        } catch (Exception e) {
            return RUtil.error(REnum.DELETE_ERR.getCode(), "批量删除成绩失败");
        }
    }

    @PostMapping("/getbypage")
    public R<Page<Score>> getByPage(@RequestBody QueryObj<Score> qObj) {
        System.out.println("接收到的请求数据: " + qObj);

        // 按id排倒序
        Sort sort = Sort.by(Sort.Direction.DESC, "id");

        Integer pageIndex = 0;
        Integer pageSize = 10;

        if (qObj == null) {
            Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);
            Page<Score> scores = scoreService.getAll(pageable);
            return RUtil.success(scores.getContent(), scores.getTotalElements());
        } else {
            pageIndex = qObj.getPage() - 1;
            pageSize = qObj.getLimit();

            if (qObj.getData() instanceof Score) {
                Score score = (Score) qObj.getData();
                Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);

                ExampleMatcher matcher = ExampleMatcher.matching()
                        .withMatcher("sno", ExampleMatcher.GenericPropertyMatchers.contains())
                        .withMatcher("sname", ExampleMatcher.GenericPropertyMatchers.contains())
                        .withMatcher("cname", ExampleMatcher.GenericPropertyMatchers.contains())
                        .withIgnoreNullValues();

                Example<Score> example = Example.of(score, matcher);
                Page<Score> scorePage = scoreService.getAll(example, pageable);

                return RUtil.success(scorePage.getContent(), scorePage.getTotalElements());
            } else {
                throw new RException(REnum.QUERY_ERR);
            }
        }
    }
}